<?php
/**
 * Object Main_Project
 * @author SillySmart
 * @copyright SillySmart
 * @package Mvc.Models.Objects
 * @see Sls.Models.Core.SLS_FrontModel
 * @since 1.0
 */
class Main_Project extends SLS_FrontModel
{
	/**
	 * Class variables
	 */
	protected $__project_id;
	protected $__pk_lang;
	protected $__project_name;
	protected $__project_name_more;
	protected $__project_url_label;
	protected $__project_url;
	protected $__project_description_title;
	protected $__project_description_label;
	protected $__project_description_value;
	protected $__project_description_url;
	protected $__project_video;
	protected $__project_home_desktop_img;
	protected $__project_home_tablet_img;
	protected $__project_img;
	protected $__project_position;
	protected $_table = "project";
	protected $_db = "main";
	protected $_primaryKey = "project_id";
	protected $_fks = array();
	public $_typeErrors = array();

	/**
	 * Constructor Main_Project
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param bool $mutlilanguage true if multilanguage content, else false
	 */
	public function __construct($multilanguage=true)
	{
		parent::__construct($multilanguage);
		$this->buildDefaultValues();
	}

	/**
	 * Build default values for specific columns
	 * @author SillySmart
	 * @copyright SillySmart
	 */
	public function buildDefaultValues()
	{
	}

	/**
	 * Set the value of project_name
	 * Errors can be catched with the public variable $this->_typeErrors['project_name']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectName($value)
	{
		if ($value === "")
		{
			$this->_typeErrors['project_name'] = "E_EMPTY";
			return false;
		}

		if (is_null($value))
		{
			$this->_typeErrors['project_name'] = "E_NULL";
			return false;
		}

		if ($this->__project_name == $value)
			return true;

		if (strlen($value) > 127)
		{
			$this->_typeErrors['project_name'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_name', $value);
		return true;
	}

	/**
	 * Set the value of project_name_more
	 * Errors can be catched with the public variable $this->_typeErrors['project_name_more']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectNameMore($value)
	{
		if (empty($value))
		{
			$this->__set('project_name_more', $value);
			return true;
		}

		if ($this->__project_name_more == $value)
			return true;

		if (strlen($value) > 127)
		{
			$this->_typeErrors['project_name_more'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_name_more', $value);
		return true;
	}

	/**
	 * Set the value of project_url_label
	 * Errors can be catched with the public variable $this->_typeErrors['project_url_label']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectUrlLabel($value)
	{
		if (empty($value))
		{
			$this->__set('project_url_label', $value);
			return true;
		}

		if ($this->__project_url_label == $value)
			return true;

		if (strlen($value) > 255)
		{
			$this->_typeErrors['project_url_label'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_url_label', $value);
		return true;
	}

	/**
	 * Set the value of project_url
	 * Errors can be catched with the public variable $this->_typeErrors['project_url']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectUrl($value)
	{
		if (empty($value))
		{
			$this->__set('project_url', $value);
			return true;
		}

		if (!SLS_String::isValidUrl($value))
		{
			$this->_typeErrors['project_url'] = "E_TYPE";
			return false;
		}

		if ($this->__project_url == $value)
			return true;

		if (strlen($value) > 255)
		{
			$this->_typeErrors['project_url'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_url', $value);
		return true;
	}

	/**
	 * Set the value of project_description_title
	 * Errors can be catched with the public variable $this->_typeErrors['project_description_title']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectDescriptionTitle($value)
	{
		if ($value === "")
		{
			$this->_typeErrors['project_description_title'] = "E_EMPTY";
			return false;
		}

		if (is_null($value))
		{
			$this->_typeErrors['project_description_title'] = "E_NULL";
			return false;
		}

		if ($this->__project_description_title == $value)
			return true;

		if (strlen($value) > 127)
		{
			$this->_typeErrors['project_description_title'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_description_title', $value);
		return true;
	}

	/**
	 * Set the value of project_description_label
	 * Errors can be catched with the public variable $this->_typeErrors['project_description_label']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectDescriptionLabel($value)
	{
		if (empty($value))
		{
			$this->__set('project_description_label', $value);
			return true;
		}

		if ($this->__project_description_label == $value)
			return true;

		if (strlen($value) > 127)
		{
			$this->_typeErrors['project_description_label'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_description_label', $value);
		return true;
	}

	/**
	 * Set the value of project_description_value
	 * Errors can be catched with the public variable $this->_typeErrors['project_description_value']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectDescriptionValue($value)
	{
		if (empty($value))
		{
			$this->__set('project_description_value', $value);
			return true;
		}

		if ($this->__project_description_value == $value)
			return true;

		if (strlen($value) > 127)
		{
			$this->_typeErrors['project_description_value'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_description_value', $value);
		return true;
	}

	/**
	 * Set the value of project_description_url
	 * Errors can be catched with the public variable $this->_typeErrors['project_description_url']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectDescriptionUrl($value)
	{
		if (empty($value))
		{
			$this->__set('project_description_url', $value);
			return true;
		}

		if (!SLS_String::isValidUrl($value))
		{
			$this->_typeErrors['project_description_url'] = "E_TYPE";
			return false;
		}

		if ($this->__project_description_url == $value)
			return true;

		if (strlen($value) > 255)
		{
			$this->_typeErrors['project_description_url'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_description_url', $value);
		return true;
	}

	/**
	 * Set the value of project_video
	 * Errors can be catched with the public variable $this->_typeErrors['project_video']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectVideo($value)
	{
		if (empty($value))
		{
			$this->__set('project_video', $value);
			return true;
		}

		if ($this->__project_video == $value)
			return true;

		if (strlen($value) > 63)
		{
			$this->_typeErrors['project_video'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_video', $value);
		return true;
	}

	/**
	 * Set the value of project_home_desktop_img
	 * Errors can be catched with the public variable $this->_typeErrors['project_home_desktop_img']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectHomeDesktopImg($value)
	{
		if (!empty($this->__project_home_desktop_img))
		{
			$this->deleteFiles(array("project_home_desktop_img"));
			$this->__project_home_desktop_img = "__Uploads/__Deprecated/".$this->__project_home_desktop_img;
			$this->save();
		}

		if (is_array($value))
		{
			if ($value["error"] == 1 || $value["error"] == 2)
			{
				$this->_typeErrors['project_home_desktop_img'] = "E_SIZE";
				return false;
			}
			else
			{
				try {
					if (!file_exists($this->_generic->getPathConfig("files").$this->_table))
						mkdir($this->_generic->getPathConfig("files").$this->_table,0755);

					$tmpName = SLS_String::substrBeforeLastDelimiter($value["tmp_name"],"/")."/".$value["name"];
					$token = substr(md5(time().substr(sha1(microtime()),0,rand(5,12))),mt_rand(1,20),10);
					$fileName = SLS_String::sanitize(SLS_String::substrBeforeLastDelimiter($value["name"],"."),"_")."_".$token;
					@rename($value["tmp_name"],$tmpName);
					$extension = pathinfo($tmpName, PATHINFO_EXTENSION);

					// Check img
					$img = new SLS_Image($tmpName);
					if ($img->getParam("existed"))
					{
						$img->transform("","",$this->_generic->getPathConfig("files").$this->_table."/".$fileName.".".$extension,$extension);

						// Check thumbs
						$xmlType = new SLS_XMLToolbox(file_get_contents($this->_generic->getPathConfig("configSls")."/types.xml"));
						$result = array_shift($xmlType->getTagsAttribute("//sls_configs/entry[@table='".$this->getDatabase()."_".$this->getTable()."' and @column='".project_home_desktop_img."' and @type='file_img']","thumbs"));
						$thumbs = unserialize(str_replace("||#||",'"',$result["attribute"]));
						if (!empty($thumbs))
						{
							for($i=0 ; $i<$count=count($thumbs) ; $i++)
							{
								$img->transform($thumbs[$i]["width"],$thumbs[$i]["height"],$this->_generic->getPathConfig("files").$this->_table."/".$fileName.$thumbs[$i]["suffix"].".".$extension,$extension);
							}
						}
					}
					else
					{
						$this->_typeErrors['project_home_desktop_img'] = "E_TYPE";
						return false;
					}
					$value = $this->_table."/".$fileName.".".$extension;
				}
				catch (Exception $e) {
					$this->_typeErrors['project_home_desktop_img'] = "E_TYPE";
					return false;
				}
			}
		}

		if ($value === "")
		{
			$this->_typeErrors['project_home_desktop_img'] = "E_EMPTY";
			return false;
		}

		if (is_null($value))
		{
			$this->_typeErrors['project_home_desktop_img'] = "E_NULL";
			return false;
		}

		if ($this->__project_home_desktop_img == $value)
			return true;

		if (strlen($value) > 255)
		{
			$this->_typeErrors['project_home_desktop_img'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_home_desktop_img', $value);
		return true;
	}

	/**
	 * Set the value of project_home_tablet_img
	 * Errors can be catched with the public variable $this->_typeErrors['project_home_tablet_img']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectHomeTabletImg($value)
	{
		if (!empty($this->__project_home_tablet_img))
		{
			$this->deleteFiles(array("project_home_tablet_img"));
			$this->__project_home_tablet_img = "__Uploads/__Deprecated/".$this->__project_home_tablet_img;
			$this->save();
		}

		if (is_array($value))
		{
			if ($value["error"] == 1 || $value["error"] == 2)
			{
				$this->_typeErrors['project_home_tablet_img'] = "E_SIZE";
				return false;
			}
			else
			{
				try {
					if (!file_exists($this->_generic->getPathConfig("files").$this->_table))
						mkdir($this->_generic->getPathConfig("files").$this->_table,0755);

					$tmpName = SLS_String::substrBeforeLastDelimiter($value["tmp_name"],"/")."/".$value["name"];
					$token = substr(md5(time().substr(sha1(microtime()),0,rand(5,12))),mt_rand(1,20),10);
					$fileName = SLS_String::sanitize(SLS_String::substrBeforeLastDelimiter($value["name"],"."),"_")."_".$token;
					@rename($value["tmp_name"],$tmpName);
					$extension = pathinfo($tmpName, PATHINFO_EXTENSION);

					// Check img
					$img = new SLS_Image($tmpName);
					if ($img->getParam("existed"))
					{
						$img->transform("","",$this->_generic->getPathConfig("files").$this->_table."/".$fileName.".".$extension,$extension);

						// Check thumbs
						$xmlType = new SLS_XMLToolbox(file_get_contents($this->_generic->getPathConfig("configSls")."/types.xml"));
						$result = array_shift($xmlType->getTagsAttribute("//sls_configs/entry[@table='".$this->getDatabase()."_".$this->getTable()."' and @column='".project_home_tablet_img."' and @type='file_img']","thumbs"));
						$thumbs = unserialize(str_replace("||#||",'"',$result["attribute"]));
						if (!empty($thumbs))
						{
							for($i=0 ; $i<$count=count($thumbs) ; $i++)
							{
								$img->transform($thumbs[$i]["width"],$thumbs[$i]["height"],$this->_generic->getPathConfig("files").$this->_table."/".$fileName.$thumbs[$i]["suffix"].".".$extension,$extension);
							}
						}
					}
					else
					{
						$this->_typeErrors['project_home_tablet_img'] = "E_TYPE";
						return false;
					}
					$value = $this->_table."/".$fileName.".".$extension;
				}
				catch (Exception $e) {
					$this->_typeErrors['project_home_tablet_img'] = "E_TYPE";
					return false;
				}
			}
		}

		if ($value === "")
		{
			$this->_typeErrors['project_home_tablet_img'] = "E_EMPTY";
			return false;
		}

		if (is_null($value))
		{
			$this->_typeErrors['project_home_tablet_img'] = "E_NULL";
			return false;
		}

		if ($this->__project_home_tablet_img == $value)
			return true;

		if (strlen($value) > 255)
		{
			$this->_typeErrors['project_home_tablet_img'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_home_tablet_img', $value);
		return true;
	}

	/**
	 * Set the value of project_img
	 * Errors can be catched with the public variable $this->_typeErrors['project_img']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param string $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectImg($value)
	{
		if (!empty($this->__project_img))
		{
			$this->deleteFiles(array("project_img"));
			$this->__project_img = "__Uploads/__Deprecated/".$this->__project_img;
			$this->save();
		}

		if (is_array($value))
		{
			if ($value["error"] == 1 || $value["error"] == 2)
			{
				$this->_typeErrors['project_img'] = "E_SIZE";
				return false;
			}
			else
			{
				try {
					if (!file_exists($this->_generic->getPathConfig("files").$this->_table))
						mkdir($this->_generic->getPathConfig("files").$this->_table,0755);

					$tmpName = SLS_String::substrBeforeLastDelimiter($value["tmp_name"],"/")."/".$value["name"];
					$token = substr(md5(time().substr(sha1(microtime()),0,rand(5,12))),mt_rand(1,20),10);
					$fileName = SLS_String::sanitize(SLS_String::substrBeforeLastDelimiter($value["name"],"."),"_")."_".$token;
					@rename($value["tmp_name"],$tmpName);
					$extension = pathinfo($tmpName, PATHINFO_EXTENSION);

					// Check img
					$img = new SLS_Image($tmpName);
					if ($img->getParam("existed"))
					{
						$img->transform("","",$this->_generic->getPathConfig("files").$this->_table."/".$fileName.".".$extension,$extension);

						// Check thumbs
						$xmlType = new SLS_XMLToolbox(file_get_contents($this->_generic->getPathConfig("configSls")."/types.xml"));
						$result = array_shift($xmlType->getTagsAttribute("//sls_configs/entry[@table='".$this->getDatabase()."_".$this->getTable()."' and @column='".project_img."' and @type='file_img']","thumbs"));
						$thumbs = unserialize(str_replace("||#||",'"',$result["attribute"]));
						if (!empty($thumbs))
						{
							for($i=0 ; $i<$count=count($thumbs) ; $i++)
							{
								$img->transform($thumbs[$i]["width"],$thumbs[$i]["height"],$this->_generic->getPathConfig("files").$this->_table."/".$fileName.$thumbs[$i]["suffix"].".".$extension,$extension);
							}
						}
					}
					else
					{
						$this->_typeErrors['project_img'] = "E_TYPE";
						return false;
					}
					$value = $this->_table."/".$fileName.".".$extension;
				}
				catch (Exception $e) {
					$this->_typeErrors['project_img'] = "E_TYPE";
					return false;
				}
			}
		}

		if ($value === "")
		{
			$this->_typeErrors['project_img'] = "E_EMPTY";
			return false;
		}

		if (is_null($value))
		{
			$this->_typeErrors['project_img'] = "E_NULL";
			return false;
		}

		if ($this->__project_img == $value)
			return true;

		if (strlen($value) > 255)
		{
			$this->_typeErrors['project_img'] = "E_LENGTH";
			return false;
		}

		$this->__set('project_img', $value);
		return true;
	}

	/**
	 * Set the value of project_position
	 * Errors can be catched with the public variable $this->_typeErrors['project_position']
	 * @author SillySmart
	 * @copyright SillySmart
	 * @param int $value
	 * @return bool true if updated, false if not
	 */
	public function setProjectPosition($value)
	{
		if (!is_int(intval($value)) || intval($value) < 0)
		{
			$this->_typeErrors['project_position'] = "E_TYPE";
			return false;
		}

		$qbd = new SLS_QueryBuilder();
		$old_project_position = $this->__project_position;
		if (empty($old_project_position))
		{
			$qbd->update()
				->from("project")
				->set("`project_position` = `project_position` + 1")
				->where($qbd->expr()->gte("project_position",$value))
				->execute();
		}
		else
		{
			if ($old_project_position != $value)
			{
				$qbd->update()
					->from("project")
					->set("`project_position` = `project_position` ".(($old_project_position < $value) ? "-" : "+")." 1")
					->where($qbd->expr()->{($old_project_position < $value) ? "gt" : "gte"}("project_position",($old_project_position < $value) ? $old_project_position : $value))
					->whereAnd($qbd->expr()->{($old_project_position < $value) ? "lte" : "lt"}("project_position",($old_project_position < $value) ? $value : $old_project_position))
					->execute();
				$qbd->update()
					->from("project")
					->set($qbd->expr()->eq("project_position",$value))
					->where($qbd->expr()->eq("project_id",$this->__project_id))
					->execute();
			}
		}

		if ($value === "")
		{
			$this->_typeErrors['project_position'] = "E_EMPTY";
			return false;
		}

		if (is_null($value))
		{
			$this->_typeErrors['project_position'] = "E_NULL";
			return false;
		}

		if ($this->__project_position == $value)
			return true;

		if (strlen($value) > 20)
		{
			$this->_typeErrors['project_position'] = "E_LENGTH";
			return false;
		}

		if (!is_numeric($value))
		{
			$this->_typeErrors['project_position'] = "E_TYPE";
			return false;
		}

		$this->__set('project_position', $value);
		return true;
	}

}
?>